package com.yunkeji.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yunkeji.common.enums.app.CheckStatus;
import com.yunkeji.dao.domain.*;
import com.yunkeji.dao.mapper.*;
import com.yunkeji.system.domain.vo.IndexLineVO;
import com.yunkeji.system.domain.vo.IndexOutLineVO;
import com.yunkeji.system.domain.vo.IndexPieVO;
import com.yunkeji.system.domain.vo.IndexUnCheckVO;
import com.yunkeji.system.mapper.AnalysesMapper;
import com.yunkeji.system.service.IAnalysesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@RequiredArgsConstructor
public class IAnalysesServiceImpl implements IAnalysesService {

    private final UserCheckMapper userCheckMapper;
    private final MomentsMapper momentsMapper;
    private final LikeWallMapper likeWallMapper;
    private final PayWithDrawMapper payWithDrawMapper;
    private final CallVoiceCheckMapper callVoiceCheckMapper;
    private final UserFeedbackMapper userFeedbackMapper;
    private final AnalysesMapper analysesMapper;

    @Override
    public IndexUnCheckVO indexUnCheck() {
        Long userCheck =
                userCheckMapper.selectCount(
                        Wrappers.lambdaQuery(UserCheck.class)
                                .eq(UserCheck::getStatus, CheckStatus.待审核.getCode()));
        Long moments =
                momentsMapper.selectCount(
                        Wrappers.lambdaQuery(Moments.class)
                                .eq(Moments::getStatus, CheckStatus.待审核.getCode()));
        Long likeWall =
                likeWallMapper.selectCount(
                        Wrappers.lambdaQuery(LikeWall.class)
                                .eq(LikeWall::getStatus, CheckStatus.待审核.getCode()));
        Long withdraw =
                payWithDrawMapper.selectCount(
                        Wrappers.lambdaQuery(PayWithDraw.class)
                                .eq(PayWithDraw::getCheckStatus, CheckStatus.待审核.getCode()));
        Long voice =
                callVoiceCheckMapper.selectCount(
                        Wrappers.lambdaQuery(CallVoiceCheck.class)
                                .eq(CallVoiceCheck::getStatus, CheckStatus.待审核.getCode()));
        Long feedback =
                userFeedbackMapper.selectCount(
                        Wrappers.lambdaQuery(UserFeedback.class)
                                .eq(UserFeedback::getStatus, CheckStatus.待审核.getCode()));
        return IndexUnCheckVO.builder()
                .userCheck(userCheck)
                .moments(moments)
                .likeWall(likeWall)
                .withdraw(withdraw)
                .voiceCheck(voice)
                .feedBack(feedback)
                .build();
    }

    /**
     * 首页折线统计图
     *
     * @author 老张
     */
    @Override
    public IndexLineVO indexLine() {
        DateTime beginOfDay = DateUtil.beginOfDay(new Date());
        return IndexLineVO.builder()
                .regLine(analysesMapper.regLine(beginOfDay))
                .callLine(analysesMapper.callLine(beginOfDay))
                .rechargeLine(analysesMapper.rechargeLine(beginOfDay))
                .build();
    }

    @Override
    public IndexPieVO indexPie() {
        DateTime beginOfDay = DateUtil.beginOfDay(new Date());
        List<HashMap<String, Object>> onlineUser = analysesMapper.onlineUser();
        BigDecimal withDraw = analysesMapper.withdraw(beginOfDay);
        BigDecimal recharge = analysesMapper.recharge(beginOfDay);

        List<HashMap<String, Object>> settle = new ArrayList<>();
        HashMap<String, Object> dataSettle1 = new HashMap<>();
        dataSettle1.put("name", "充值");
        dataSettle1.put("value", recharge);
        settle.add(dataSettle1);
        HashMap<String, Object> dataSettle2 = new HashMap<>();
        dataSettle2.put("name", "提现");
        dataSettle2.put("value", withDraw);
        settle.add(dataSettle2);

        Integer callFail = analysesMapper.callFail(beginOfDay);
        Integer callSuccess = analysesMapper.callSuccess(beginOfDay);
        List<HashMap<String, Object>> call = new ArrayList<>();
        HashMap<String, Object> dataCall1 = new HashMap<>();
        dataCall1.put("name", "已接通");
        dataCall1.put("value", callSuccess);
        call.add(dataCall1);
        HashMap<String, Object> dataCall2 = new HashMap<>();
        dataCall2.put("name", "未接通");
        dataCall2.put("value", callFail);
        call.add(dataCall2);

        return IndexPieVO.builder().call(call).settle(settle).onlineUser(onlineUser).build();
    }

    @Override
    public IndexOutLineVO indexOutline() {
        Date date = new Date();
        // 今天
        DateTime todayStart = DateUtil.beginOfDay(date);
        DateTime todayEnd = DateUtil.endOfDay(date);
        // 昨天
        DateTime dateTime = DateUtil.offsetDay(date, -1);
        DateTime yesterdayStart = DateUtil.beginOfDay(dateTime);
        DateTime yesterdayEnd = DateUtil.endOfDay(dateTime);

        BigDecimal sumTodayRecharge = analysesMapper.sumRecharge(todayStart, todayEnd);
        BigDecimal sumyesterdayRecharge = analysesMapper.sumRecharge(yesterdayStart, yesterdayEnd);

        BigDecimal sumTodayWithdraw = analysesMapper.sumWithdraw(todayStart, todayEnd);
        BigDecimal sumyesterdayWithdraw = analysesMapper.sumWithdraw(yesterdayStart, yesterdayEnd);

        BigDecimal sumTodayCall = analysesMapper.sumCall(todayStart, todayEnd)  .divide(BigDecimal.valueOf(60), 0, RoundingMode.UP);

        BigDecimal sumyesterdayCall = analysesMapper.sumCall(yesterdayStart, yesterdayEnd)  .divide(BigDecimal.valueOf(60), 0, RoundingMode.UP);

        Integer sumTodayReg = analysesMapper.sumReg(todayStart, todayEnd);
        Integer sumyesterdayReg = analysesMapper.sumReg(yesterdayStart, yesterdayEnd);

        return IndexOutLineVO.builder()
                .sumyesterdayReg(sumyesterdayReg)
                .sumTodayReg(sumTodayReg)
                .sumyesterdayCall(sumyesterdayCall.intValue())
                .sumTodayCall(sumTodayCall.intValue())
                .sumyesterdayWithdraw(sumyesterdayWithdraw)
                .sumTodayWithdraw(sumTodayWithdraw)
                .sumyesterdayRecharge(sumyesterdayRecharge)
                .sumTodayRecharge(sumTodayRecharge)
                .build();
    }
}
